<?php
namespace App\Http\Controllers\admin;
use Illuminate\Support\Facades\DB;
use Session;
class NodeController extends CommonController{
    public function node_list(){
        $data = DB::table('mo_role')->where('r_id','!=',1)->paginate(5);
        return view('admin.node.node_list',compact('data'));
    }
    public function away($id){
        $row = DB::table('mo_role')->where(['r_id'=>$id])->first();
        $data1 = DB::table('mo_node')->where(['n_id'=>$id])->get();
        $data2 = DB::table('mo_sql')->get();
        return view('admin.node.away',compact('row','data1','data2','id'));
    }
    public function module($rid,$str="",$selected_val){
        Session::put('role_id',$rid);
        Session::put('node_id',$selected_val);
        DB::table('mo_access')->where(["role_id" =>$rid,'node_id'=>$selected_val])->delete();
        $str = explode(",",$str);
        foreach($str as $v){
            DB::table('mo_access')->insert([
                'role_id'=>$rid,
                'node_id'=>$selected_val,
                'sq_id'=>$v
            ]);
        }
        return Redirect('node/node_list');
    }
    public function module_res($rid,$selected_val){
        $res = DB::table('mo_access')->where(["role_id" =>$rid,'node_id'=>$selected_val])->delete();
        if($res){
            return Redirect('node/node_list');
        }else{
            return Redirect('node/node_list');
        }
    }
    public function middle($role_id,$node_id){
        $data = DB::table('mo_sql')->get();
        $num = DB::table('mo_access')->select('sq_id')->where(['role_id'=>$role_id,'node_id'=>$node_id])->get();
        if(empty($num)){
            return view('admin.node.a',compact('data'));
        }else{
            foreach($num as $k=>$v){
                $nums[] = $v->sq_id;
            }
            return view('admin.node.away_s',compact('data','nums'));
        }
    }
}
?>